package com.example.yls.demoa;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private ListView listView;
    private final String TAG="MainActivity";
    private ArrayList<Student>studentArrayList=new ArrayList<>();
    private MyAdapter adapter;
   private Button btnAddStu;
    private String[] namerr={"one","two","three","four"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initStuList();



        listView=findViewById(R.id.listView);

        adapter=new MyAdapter(MainActivity.this,studentArrayList);
        listView.setAdapter(adapter);

        btnAddStu=findViewById(R.id.btn_addStu);
        btnAddStu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent();
                intent.setClass(MainActivity.this,AddActivity.class);
                startActivityForResult(intent,1001);


            }
        });

    }

    protected void onActivityResult(int requestCode,int resultCode,Intent data){
        Log.e("MainActivity","requestCode="+requestCode+"resultCode="+
                resultCode);
        if(data==null){
            return;
        }

        String name=data.getStringExtra("NAME");
        int age=data.getIntExtra("AGE",0);
        int imgId=data.getIntExtra("IMG",R.drawable.one);

        Student s=new Student(name,age,imgId);
        studentArrayList.add(s);
        adapter.notifyDataSetChanged();

    }

    private void initStuList() {
        Student s1=new Student(R.drawable.one,"张三",18);
        Student s2=new Student(R.drawable.two,"李四",19);
        Student s3=new Student(R.drawable.three,"王五",20);
        Student s4=new Student(R.drawable.four,"牦牛",21);
        studentArrayList.add(s1);
        studentArrayList.add(s2);
        studentArrayList.add(s3);
        studentArrayList.add(s4);



    }
}